﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO.Compression" %>

<script runat="server">  
    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
      MyConfig.serverPath = Server.MapPath("~/");
      //Application.Add("online", 0);
      //Application.Add("total", Myfunction.GetVisited()); 
      Application["HomNay"] = 0;
      Application["HomQua"] = 0;
      Application["TuanNay"] = 0;
      Application["TuanTruoc"] = 0;
      Application["ThangNay"] = 0;
      Application["ThangTruoc"] = 0;
      Application["TatCa"] = 0;
      Application["visitors_online"] = 0;
    
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
      // Application.Remove("online");
      //int total = (int)Application["total"];
      //Myfunction.SaveVisited(total);

      Session.Timeout = 150;
      Application.Lock();
      Application["visitors_online"] = Convert.ToInt32(Application["visitors_online"]) + 1;
      Application.UnLock();
      try
      {
        DataBindSQL mThongKe = new DataBindSQL();
        DataTable dtb = mThongKe.TableThongKe();
        if (dtb.Rows.Count > 0)
        {
          Application["HomNay"] = long.Parse("0" + dtb.Rows[0]["HomNay"]).ToString("#,###");
          Application["HomQua"] = long.Parse("0" + dtb.Rows[0]["HomQua"]).ToString("#,###");
          Application["TuanNay"] = long.Parse("0" + dtb.Rows[0]["TuanNay"]).ToString("#,###");
          Application["TuanTruoc"] = long.Parse("0" + dtb.Rows[0]["TuanTruoc"]).ToString("#,###");
          Application["ThangNay"] = long.Parse("0" + dtb.Rows[0]["ThangNay"]).ToString("#,###");
          Application["ThangTruoc"] = long.Parse("0" + dtb.Rows[0]["ThangTruoc"]).ToString("#,###");
          Application["TatCa"] = long.Parse("0" + dtb.Rows[0]["TatCa"]).ToString("#,###");
        }
        dtb.Dispose();
        mThongKe = null;
      }
      catch { }
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
      // int online = (int)Application["online"];
      //online++;
      //Application["online"] = online;

      //int total = (int)Application["total"];
      //total++;
      //Application["total"] = total;

      //Myfunction.GetDate();

      Session.Timeout = 150;
      Application.Lock();
      Application["visitors_online"] = Convert.ToInt32(Application["visitors_online"]) + 1;
      Application.UnLock();
      try
      {
        DataBindSQL mThongKe = new DataBindSQL();
        DataTable dtb = mThongKe.TableThongKe();
        if (dtb.Rows.Count > 0)
        {
          Application["HomNay"] = long.Parse("0" + dtb.Rows[0]["HomNay"]).ToString("#,###");
          Application["HomQua"] = long.Parse("0" + dtb.Rows[0]["HomQua"]).ToString("#,###");
          Application["TuanNay"] = long.Parse("0" + dtb.Rows[0]["TuanNay"]).ToString("#,###");
          Application["TuanTruoc"] = long.Parse("0" + dtb.Rows[0]["TuanTruoc"]).ToString("#,###");
          Application["ThangNay"] = long.Parse("0" + dtb.Rows[0]["ThangNay"]).ToString("#,###");
          Application["ThangTruoc"] = long.Parse("0" + dtb.Rows[0]["ThangTruoc"]).ToString("#,###");
          Application["TatCa"] = long.Parse("0" + dtb.Rows[0]["TatCa"]).ToString("#,###");
        }
        dtb.Dispose();
        mThongKe = null;
      }
      catch { }
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
      //int online = (int)Application["online"];
      //online--;
      //Application["online"] = online; 

      Application.Lock();
      Application["visitors_online"] = Convert.ToUInt32(Application["visitors_online"]) - 1;
      Application.UnLock();
    }

    //protected void Application_BeginRequest(Object sender, EventArgs e)
    //{
    //    if (Request.Path.IndexOf(".asmx") != -1) // if the url contains .asmx extention
    //    {

    //        SendRequestWithSimpleURL(Context);

    //        return;
    //    }
    //    else
    //    {


    //        ThunderMain.URLRewriter.Rewriter.Process(); // this is my URL rewriter Code. Paste your Code for url rewriting
    //    }

    //}

    //private static void SendRequestWithSimpleURL(HttpContext context)
    //{

    //    int asmx = context.Request.Path.IndexOf(".asmx");
    //    string path = context.Request.Path.Substring(0, asmx + 5);

    //    string pathInfo = context.Request.Path.Substring(asmx + 5);
    //    context.RewritePath(path, pathInfo, context.Request.Url.Query);

    //}
    
</script>
